package com.company.psm.serviceimpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.company.psm.entity.Category;
import com.company.psm.entity.Gradient;
import com.company.psm.entity.Medicine;
import com.company.psm.entity.Store;
import com.company.psm.genericdao.CustomGenericDAO;
import com.company.psm.service.MedicineManagement;

@Service(value="medicineManagement")
@Transactional(readOnly = true, propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class MedicineManagementImpl extends CustomGenericDAO<Medicine> implements MedicineManagement  {

	@Transactional(readOnly = true)
	public List<Medicine> getAllMedicines() {
		// TODO Auto-generated method stub
		List<Medicine>result = _all(Medicine.class);
		return result;
	}

	


	public Medicine find(Long arg0) {
		// TODO Auto-generated method stub
		return null;
	}


	public Medicine[] find(Long... arg0) {
		// TODO Auto-generated method stub
		return null;
	}


	public Medicine getReference(Long arg0) {
		// TODO Auto-generated method stub
		return null;
	}


	public Medicine[] getReferences(Long... arg0) {
		// TODO Auto-generated method stub
		return null;
	}


	public boolean removeById(Long arg0) {
		// TODO Auto-generated method stub
		return false;
	}


	public void removeByIds(Long... arg0) {
		// TODO Auto-generated method stub
		
	}



	@Transactional(readOnly = false)
	public void createNewMedicine(String name_, String description_,
			String price_, String quantity_, long storeId_,List<Gradient>list_,String specialize, String contraindicated, Category category) {
		// TODO Auto-generated method stub
		Medicine medicine = new Medicine();
		medicine.setName(name_);
		medicine.setDescription(description_);
		medicine.setPrice(price_);
		medicine.setQuantity(quantity_);
		Store store = _load(Store.class, storeId_);
		medicine.setStore(store);
		medicine.setGradients(list_);
		medicine.setContraindicated(specialize);
		medicine.setContraindicated(contraindicated);
		medicine.setCategory(category);
		_save(medicine);
	}



	@SuppressWarnings("null")
	@Transactional(readOnly = true)
	public List<String> getMedicineNameListByStoreId(long storeId_) {
		// TODO Auto-generated method stub
		List<String>result = null;
		List<Medicine> allMedicines = this.getAllMedicines();
		for (Medicine medicine : allMedicines) {
			if(medicine.getStore().getId() == storeId_){
				result.add(medicine.getName());
			}
		}
		return result;
	}
	@Transactional(readOnly = false)
	public List<Medicine> search(String searchKey, Long storeId) {
		List<Medicine> listSearchMedicines = new ArrayList<Medicine>();
		List<Medicine> medicines = getMedicineByStoreId(storeId);
		if(searchKey!=null){
			for (Medicine medicine : medicines) {
					if(medicine.getName().toString().toUpperCase().contains(searchKey.toString().toUpperCase())){
						listSearchMedicines.add(medicine);
					}
			}
		}else{
			listSearchMedicines = medicines;
		}
		return listSearchMedicines;
	}



	@Transactional(readOnly = false)
	public void updateMedicine(String id, String name, String description,
			String price, String quantity,long storeId,
			List<Gradient> gradients, String specialize, String contraindicated, Category category) {
		Store store = _get(Store.class, storeId);
		Medicine medicine = _get(Medicine.class, Long.parseLong(id));
		medicine.setDescription(description);
		medicine.setGradients(gradients);
		medicine.setName(name);
		medicine.setPrice(price);
		medicine.setQuantity(quantity);
		medicine.setStore(store);
		medicine.setSpecialize(specialize);
		medicine.setContraindicated(contraindicated);
		medicine.setCategory(category);
		_update(medicine);
	}

	public String getMedicinePriceById(long medicineId) {
		String result = "";
		List<Medicine> medicineList = this.getAllMedicines();
		for (Medicine medicine : medicineList) {
			if (medicine.getId() == medicineId) {
				result = medicine.getPrice();
			}
		}
		return result;
	}

	public List<Medicine> getMedicineByStoreId(long storeId) {
		List<Medicine> medicines = new ArrayList<Medicine>();
		for (Medicine medicine : getAllMedicines()) {
			if(medicine.getStore().getId() == storeId){
				medicines.add(medicine);
			}
		}
		return medicines;
	}
	public Medicine getMedicineById(long id){
		return _get(Medicine.class,id);
	}




	public Medicine getMedicineByName(String name) {
		// TODO Auto-generated method stub
		Medicine result = new Medicine();
		List<Medicine> medicines = this.findAll();
		for (Medicine medicine : medicines) {
			if( medicine.getName().equals(name)){
				result = medicine;
			}
		}
		return result;
	}
	@Transactional(readOnly = false)
	public void addMedicineToStore(String medicineId, Long storeId) {
		Medicine medicine = _get(Medicine.class, Long.parseLong(medicineId));
		List<Gradient> gradients = medicine.getGradients();
		List<Gradient> getGradients = new ArrayList<Gradient>();
		for (Gradient gradient : gradients) {
			getGradients.add(gradient);
		}
		Medicine newMedicine = new Medicine();
		newMedicine.setName(medicine.getName());
		newMedicine.setDescription(medicine.getDescription());
		newMedicine.setPrice("0");
		newMedicine.setQuantity("0");
		Store store = _load(Store.class, storeId);
		newMedicine.setStore(store);
		newMedicine.setGradients(getGradients);
		newMedicine.setSpecialize(medicine.getSpecialize());
		newMedicine.setContraindicated(medicine.getContraindicated());
		_save(newMedicine);
	}

	@Transactional(readOnly = false)
	public void deleteMedicine(long id) {
		try{
			System.out.println("called!");
		_deleteById(Medicine.class, id);
		System.out.println("deleted!");
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}
